package bdd.daos.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import bdd.beans.DBLink;
import bdd.beans.Enseignant;
import bdd.daos.DAOEnseignant;
import bdd.daos.DAORole;

public class DAOEnseignantImpl implements DAOEnseignant{

	private DBLink dblink;
	
	public DAOEnseignantImpl() {
		this.dblink = new DBLink();
	}
	
	@Override
	public Enseignant save(Enseignant newInstance) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Enseignant find(Integer id) {
		dblink.connexion();
		
		String query = "select * from enseignant where numenseignant = "+id;
		
		try {
			PreparedStatement pst = dblink.getConnection().prepareStatement(query); 
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()) {
				Enseignant e = new Enseignant();
				e.setAdresse(rs.getString("ADRESSE"));
				e.setDatenaissance(rs.getDate("DATENAISSANCE"));
				e.setLogin(rs.getString("LOGIN"));
				e.setNom(rs.getString("NOM"));
				e.setNumenseignant(rs.getInt("NUMENSEIGNANT"));
				e.setPrenom(rs.getString("PRENOM"));
				e.setPwd(rs.getString("PWD"));
				e.setTelephone(rs.getLong("TELEPHONE"));
				
				DAORole daoRole = new DAORoleImpl();
				e.setRole(daoRole.find(rs.getInt("IDROLE")));
				
				DAOTypeposteImpl daoTypeposte = new DAOTypeposteImpl();
				e.setTypeposte(daoTypeposte.find(rs.getInt("NUMPOSTE")));
				
				e.setCreneaus(null);
				e.setIndisponibiliteses(null);
				e.setJnttypeecenseignants(null);
				
				pst.close();
				
				dblink.deconnexion();
				
				return e;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		dblink.deconnexion();
		
		return null;
	}

	@Override
	public List<Enseignant> findAll() {
		
		return null;
	}

	@Override
	public void update(Enseignant transientObject) {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(Enseignant persistentObject) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Set<Enseignant> findByRole(Integer idRole) {
		Set<Enseignant> liste = new HashSet<Enseignant>();
		dblink.connexion();
		
		String query = "select * from enseignant where idrole = "+idRole;
		
		try {
			PreparedStatement pst = dblink.getConnection().prepareStatement(query); 
			ResultSet rs = pst.executeQuery();
			
			while(rs.next()) {
				Enseignant e = new Enseignant();
				e.setAdresse(rs.getString("ADRESSE"));
				e.setDatenaissance(rs.getDate("DATENAISSANCE"));
				e.setLogin(rs.getString("LOGIN"));
				e.setNom(rs.getString("NOM"));
				e.setNumenseignant(rs.getInt("NUMENSEIGNANT"));
				e.setPrenom(rs.getString("PRENOM"));
				e.setPwd(rs.getString("PWD"));
				e.setTelephone(rs.getLong("TELEPHONE"));
				
				DAORole daoRole = new DAORoleImpl();
				e.setRole(daoRole.find(rs.getInt("IDROLE")));
				
				DAOTypeposteImpl daoTypeposte = new DAOTypeposteImpl();
				e.setTypeposte(daoTypeposte.find(rs.getInt("NUMPOSTE")));
				
				e.setCreneaus(null);
				e.setIndisponibiliteses(null);
				e.setJnttypeecenseignants(null);
			}
			
			pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			dblink.deconnexion();
		}
		
		return liste;
	}
}
